<div id="container">
<div id="sample" contenteditable="true"></div>
</div>
<script src="../../resources/js-test.js"></script>
<script>
function $(id) { return document.getElementById(id); }
var sample = $('sample');
var selection = window.getSelection();
function testIt(sourceHTML, expectedHTML, expectedNode, expectedOffset)
{
    var sourceText = sourceHTML.replace(/<br>/g, '\n');
    var caretPos = sourceText.indexOf('|');
    sample.innerHTML = sourceHTML.replace('|', '');
    sample.focus();
    var range = document.createRange();
    if (caretPos == sample.childNodes.length) {
        range.selectNodeContents(sample);
        range.collapse(false);
    } else {
        range.setStartBefore(sample.childNodes[caretPos]);
    }
    selection.removeAllRanges();
    selection.addRange(range);
    document.execCommand('Delete', false);

    debug(sourceHTML.replace(/</g, '&lt;'));
    shouldBeEqualToString('sample.innerHTML', expectedHTML);
    shouldBeEqualToString('selection.type', 'Caret');
    shouldBe('selection.focusNode', expectedNode);
    shouldBe('selection.focusOffset', expectedOffset.toString());
    debug('');
}

testIt('a|<br>', '<br>', 'sample', 0);
testIt('a<br>|<br>', 'a<br>', 'sample.firstChild', 1);
testIt('a<br><br>|<br>', 'a<br><br>', 'sample', 2);

testIt('a|<br><br>', '<br><br>', 'sample', 0);
testIt('a<br>|<br><br>', 'a<br><br>', 'sample.firstChild', 1);
testIt('a<br><br>|<br><br>', 'a<br><br><br>', 'sample', 2);

testIt('a<br>|b', 'ab', 'sample.firstChild', 1);
testIt('a<br><br>|b', 'a<br>b', 'sample.lastChild', 0);
testIt('a<br><br><br>|b', 'a<br><br>b', 'sample.lastChild', 0);

testIt('a<br>b|', 'a<br><br>', 'sample', 2);
testIt('a<br><br>b|', 'a<br><br><br>', 'sample', 3);
testIt('a<br><br><br>b|', 'a<br><br><br><br>', 'sample', 4);

if (window.testRunner)
    $('container').outerHTML = '';
</script>
